package com.doctor.adapter;

import java.io.Serializable;
import java.util.List;

import com.doctor.bean.ArticleBean;
import com.doctor.bean.KnowledgeBean;
import com.doctor.comm.MyApplication;
import com.doctor.ui.R;
import com.doctor.view.BadgeView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class KnowledgeFourthAdapter extends BaseExpandableListAdapter{

	/**
	 * 医学知识医疗技术四五级目录adapter
	 */
	
	private Context context;
	private List<KnowledgeBean> dirList;
	private List<List<ArticleBean>> articleList;
	private int fourthSize;
	//新标志所需长度
	private int newFlagPadding ;
	
	
	public KnowledgeFourthAdapter(Context context){
		this.context=context;
		newFlagPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 26, MyApplication.getInstance().getResources().getDisplayMetrics());
	}
	
	public KnowledgeFourthAdapter(Context context, int fourthSize, List<KnowledgeBean> dirList, 
			List<List<ArticleBean>> articleList){
		this.context=context;
		this.fourthSize=fourthSize;
		this.dirList=dirList;
		this.articleList=articleList;
		newFlagPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 26, MyApplication.getInstance().getResources().getDisplayMetrics());
	}
	
	@Override
	public ArticleBean getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		if (arg0>fourthSize-1) {
			return null;
		} else {
			List<ArticleBean> list=articleList.get(arg0);
			if (list!=null && list.size()>0) {
				return list.get(arg1);
			}
		}
		return null;
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return arg1;
	}

	@Override
	public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
			ViewGroup arg4) {
		// TODO Auto-generated method stub
		ChildViewHolder holder;
		if (arg3==null) {
			arg3=View.inflate(context, R.layout.knowledge_five_item, null);
			holder = new ChildViewHolder();
			holder.five = (TextView) arg3.findViewById(R.id.name);
			holder.newFlag = new BadgeView(context, holder.five);
			holder.newFlag.setText("new");
			holder.newFlag.setPadding(0, 0, 0, 0);
			holder.newFlag.setTypeface(Typeface.DEFAULT);
			holder.newFlag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			holder.newFlag.setTextColor(Color.RED);
			holder.newFlag.setBackgroundColor(Color.TRANSPARENT);
			
			arg3.setTag(holder);
		} else {
			holder = (ChildViewHolder) arg3.getTag();
		}
		if (arg0<fourthSize) {
			List<ArticleBean> art=articleList.get(arg0);
			if (art!=null && art.size()>0) {
				ArticleBean bean=art.get(arg1);
				holder.five.setText(bean.getTitle());
				
				boolean isNew = bean.getIsNew();
				if (isNew) {
					holder.five.setPadding(0, 0, newFlagPadding, 0);
					holder.newFlag.show();
				} else {
					holder.five.setPadding(0, 0, 0, 0);
					holder.newFlag.hide();
				}
				
				arg3.setTag(R.id.id_tag, bean.getId());
				arg3.setTag(R.id.video_url_tag, bean.getJj());
				arg3.setTag(R.id.group_position_tag, arg0);
				arg3.setTag(R.id.child_position_tag, arg1);
			}
		}
		
		return arg3;
	}
	
	
	private class ChildViewHolder {
		TextView five;
		BadgeView newFlag;
	}
	

	@Override
	public int getChildrenCount(int arg0) {
		// TODO Auto-generated method stub
		if (arg0<fourthSize) {
			List<ArticleBean> list=articleList.get(arg0);
			if (list!=null && list.size()>0) {
				return list.size();
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	@Override
	public Serializable getGroup(int arg0) {
		// TODO Auto-generated method stub
		Serializable bean;
		if (arg0 < fourthSize) {
			KnowledgeBean kno = dirList.get(arg0);
			bean = kno;
		} else {
			ArticleBean art = articleList.get(fourthSize).get(arg0-fourthSize);
			bean = art;
		}
		return bean;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		int count = 0;
		if (fourthSize>0 && articleList.size()==fourthSize) {
			//group全是目录
			count=fourthSize;
		} else if(fourthSize<=0 && articleList.size()>0) {
			//group全是文章
			count=articleList.get(0).size();
		} else if (fourthSize>0 && articleList.size()>fourthSize) {
			//group既有目录也有文章
			count=fourthSize+articleList.get(fourthSize).size();
		}
		return count;
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
		// TODO Auto-generated method stub
		GroupViewHolder holder;
		if (arg2==null) {
			arg2=View.inflate(context, R.layout.knowledge_fourth_item, null);
			holder = new GroupViewHolder();
			holder.fourth = (TextView) arg2.findViewById(R.id.name);
			holder.newFlag = new BadgeView(context, holder.fourth);
			holder.newFlag.setText("new");
			holder.newFlag.setPadding(0, 0, 0, 0);
			holder.newFlag.setTypeface(Typeface.DEFAULT);
			holder.newFlag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			holder.newFlag.setTextColor(Color.RED);
			holder.newFlag.setBackgroundColor(Color.TRANSPARENT);
			
			arg2.setTag(holder);
		} else {
			holder = (GroupViewHolder) arg2.getTag();
		}
		
		if (arg0<fourthSize) {
			KnowledgeBean kno=dirList.get(arg0);
			holder.fourth.setText(kno.getDirname());
			
			boolean isNew = kno.getIsNew();
			if (isNew) {
				holder.fourth.setPadding(0, 0, newFlagPadding, 0);
				holder.newFlag.show();
			} else {
				holder.fourth.setPadding(0, 0, 0, 0);
				holder.newFlag.hide();
			}
			
			arg2.setTag(R.id.bean_tag, "kno");
		} else {
			ArticleBean art=articleList.get(fourthSize).get(arg0-fourthSize);
			holder.fourth.setText(art.getTitle());
			
			boolean isNew = art.getIsNew();
			if (isNew) {
				holder.fourth.setPadding(0, 0, newFlagPadding, 0);
				holder.newFlag.show();
			} else {
				holder.fourth.setPadding(0, 0, 0, 0);
				holder.newFlag.hide();
			}
			
			arg2.setTag(R.id.id_tag,art.getId());
			arg2.setTag(R.id.bean_tag,"art");
			arg2.setTag(R.id.group_position_tag, arg0);
		}
		return arg2;
	}
	
	
	private class GroupViewHolder {
		TextView fourth;
		BadgeView newFlag;
	}
	
	

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}
	
	public List<KnowledgeBean> getDirList(){
		return dirList;
	}
	
	public List<List<ArticleBean>> getArticleList(){
		return articleList;
	}
	
	
	

}
